package com.centit.springbootdemo03.controller;

import com.alibaba.fastjson.JSON;
import com.centit.springbootdemo03.entity.User;
import com.centit.springbootdemo03.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhao_jx
 * @date 2020/7/5 9:53
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/one/{id}")
    public String addUser(@PathVariable("id") Long id){
        User user=new User();
        user.setId(id);
        user.setName("张三");
        user.setAge(18);
        user.setEmail("zhanagsan@centit.com");
        int rs = userService.addUser(user);
        return "添加成功" + rs;
    }

    @GetMapping("/all")
    public String queryAll(){
        List<User> users = userService.queryAllUser(new User());
        return JSON.toJSONString(users,true);
    }

    @GetMapping("/one/{id}")
    public String queryOne(@PathVariable("id") Long id){
        User user = userService.getUserById(id);
        return JSON.toJSONString(user,true);
    }
}
